// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.textures;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import zombie.debug.DebugLog;

public class Pcx {
    public static HashMap<String, Pcx> Cache = new HashMap();
    public byte[] imageData;
    public int imageWidth;
    public int imageHeight;
    public int[] palette;
    public int[] pic;

    public Pcx(String file) {
    }

    public Pcx(URL url) {
    }

    public Pcx(String var1, int[] var2) {
    }

    public Pcx(String url, String urlPal) {
    }

    public Image getImage() {
        int[] int0 = new int[this.imageWidth * this.imageHeight];
        int int1 = 0;
        int int2 = 0;

        for (int int3 = 0; int3 < this.imageWidth; int3++) {
            for (int int4 = 0; int4 < this.imageHeight; int4++) {
                int0[int1++] = 0xFF000000 | (this.imageData[int2++] & 255) << 16 | (this.imageData[int2++] & 255) << 8 | this.imageData[int2++] & 255;
            }
        }

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        return toolkit.createImage(new MemoryImageSource(this.imageWidth, this.imageHeight, int0, 0, this.imageWidth));
    }

    int loadPCX(URL uRL) {
        try {
            InputStream inputStream = uRL.openStream();
            int int0 = inputStream.available();
            byte[] byte0 = new byte[int0 + 1];
            byte0[int0] = 0;

            for (int int1 = 0; int1 < int0; int1++) {
                byte0[int1] = (byte)inputStream.read();
            }

            inputStream.close();
            if (int0 == -1) {
                return -1;
            } else {
                Pcx.pcx_t pcx_t = new Pcx.pcx_t(byte0);
                byte[] byte1 = pcx_t.data;
                if (pcx_t.manufacturer == '\n'
                    && pcx_t.version == 5
                    && pcx_t.encoding == 1
                    && pcx_t.bits_per_pixel == '\b'
                    && pcx_t.xmax < 640
                    && pcx_t.ymax < 480) {
                    this.palette = new int[768];

                    for (int int2 = 0; int2 < 768; int2++) {
                        if (int0 - 128 - 768 + int2 < pcx_t.data.length) {
                            this.palette[int2] = pcx_t.data[int0 - 128 - 768 + int2] & 255;
                        }
                    }

                    this.imageWidth = pcx_t.xmax + 1;
                    this.imageHeight = pcx_t.ymax + 1;
                    int[] int3 = new int[(pcx_t.ymax + 1) * (pcx_t.xmax + 1)];
                    this.pic = int3;
                    int[] int4 = int3;
                    int int5 = 0;
                    int int6 = 0;

                    for (int int7 = 0; int7 <= pcx_t.ymax; int5 += pcx_t.xmax + 1) {
                        int int8 = 0;

                        while (int8 <= pcx_t.xmax) {
                            byte byte2 = byte1[int6++];
                            int int9;
                            if ((byte2 & 192) == 192) {
                                int9 = byte2 & 63;
                                byte2 = byte1[int6++];
                            } else {
                                int9 = 1;
                            }

                            while (int9-- > 0) {
                                int4[int5 + int8++] = byte2 & 255;
                            }
                        }

                        int7++;
                    }

                    if (this.pic != null && this.palette != null) {
                        this.imageData = new byte[(this.imageWidth + 1) * (this.imageHeight + 1) * 3];

                        for (int int10 = 0; int10 < this.imageWidth * this.imageHeight; int10++) {
                            this.imageData[int10 * 3] = (byte)this.palette[this.pic[int10] * 3];
                            this.imageData[int10 * 3 + 1] = (byte)this.palette[this.pic[int10] * 3 + 1];
                            this.imageData[int10 * 3 + 2] = (byte)this.palette[this.pic[int10] * 3 + 2];
                        }

                        return 1;
                    } else {
                        return -1;
                    }
                } else {
                    DebugLog.log("Bad pcx file " + uRL);
                    return -1;
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            return 1;
        }
    }

    int loadPCXminusPal(String string) {
        try {
            if (Cache.containsKey(string)) {
                Pcx pcx0 = (Pcx)Cache.get(string);
                this.imageWidth = pcx0.imageWidth;
                this.imageHeight = pcx0.imageHeight;
                this.imageData = new byte[(pcx0.imageWidth + 1) * (pcx0.imageHeight + 1) * 3];

                for (int int0 = 0; int0 < pcx0.imageWidth * pcx0.imageHeight; int0++) {
                    this.imageData[int0 * 3] = (byte)this.palette[pcx0.pic[int0] * 3];
                    this.imageData[int0 * 3 + 1] = (byte)this.palette[pcx0.pic[int0] * 3 + 1];
                    this.imageData[int0 * 3 + 2] = (byte)this.palette[pcx0.pic[int0] * 3 + 2];
                }

                return 1;
            } else {
                InputStream inputStream = zombie.GameWindow.class.getClassLoader().getResourceAsStream(string);
                if (inputStream == null) {
                    return 0;
                } else {
                    int int1 = inputStream.available();
                    byte[] byte0 = new byte[int1 + 1];
                    byte0[int1] = 0;

                    for (int int2 = 0; int2 < int1; int2++) {
                        byte0[int2] = (byte)inputStream.read();
                    }

                    inputStream.close();
                    if (int1 == -1) {
                        return -1;
                    } else {
                        Pcx.pcx_t pcx_t = new Pcx.pcx_t(byte0);
                        byte[] byte1 = pcx_t.data;
                        if (pcx_t.manufacturer == '\n'
                            && pcx_t.version == 5
                            && pcx_t.encoding == 1
                            && pcx_t.bits_per_pixel == '\b'
                            && pcx_t.xmax < 640
                            && pcx_t.ymax < 480) {
                            this.imageWidth = pcx_t.xmax + 1;
                            this.imageHeight = pcx_t.ymax + 1;
                            int[] int3 = new int[(pcx_t.ymax + 1) * (pcx_t.xmax + 1)];
                            this.pic = int3;
                            int[] int4 = int3;
                            int int5 = 0;
                            int int6 = 0;

                            for (int int7 = 0; int7 <= pcx_t.ymax; int5 += pcx_t.xmax + 1) {
                                int int8 = 0;

                                while (int8 <= pcx_t.xmax) {
                                    byte byte2 = byte1[int6++];
                                    int int9;
                                    if ((byte2 & 192) == 192) {
                                        int9 = byte2 & 63;
                                        byte2 = byte1[int6++];
                                    } else {
                                        int9 = 1;
                                    }

                                    while (int9-- > 0) {
                                        int4[int5 + int8++] = byte2 & 255;
                                    }
                                }

                                int7++;
                            }

                            if (this.pic != null && this.palette != null) {
                                this.imageData = new byte[(this.imageWidth + 1) * (this.imageHeight + 1) * 3];

                                for (int int10 = 0; int10 < this.imageWidth * this.imageHeight; int10++) {
                                    this.imageData[int10 * 3] = (byte)this.palette[this.pic[int10] * 3];
                                    this.imageData[int10 * 3 + 1] = (byte)this.palette[this.pic[int10] * 3 + 1];
                                    this.imageData[int10 * 3 + 2] = (byte)this.palette[this.pic[int10] * 3 + 2];
                                }

                                Cache.put(string, this);
                                return 1;
                            } else {
                                return -1;
                            }
                        } else {
                            DebugLog.log("Bad pcx file " + string);
                            return -1;
                        }
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            return 1;
        }
    }

    int loadPCXpal(String string) {
        try {
            InputStream inputStream = zombie.GameWindow.class.getClassLoader().getResourceAsStream(string);
            if (inputStream == null) {
                return 1;
            } else {
                int int0 = inputStream.available();
                byte[] byte0 = new byte[int0 + 1];
                byte0[int0] = 0;

                for (int int1 = 0; int1 < int0; int1++) {
                    byte0[int1] = (byte)inputStream.read();
                }

                inputStream.close();
                if (int0 == -1) {
                    return -1;
                } else {
                    Pcx.pcx_t pcx_t = new Pcx.pcx_t(byte0);
                    byte[] byte1 = pcx_t.data;
                    if (pcx_t.manufacturer == '\n'
                        && pcx_t.version == 5
                        && pcx_t.encoding == 1
                        && pcx_t.bits_per_pixel == '\b'
                        && pcx_t.xmax < 640
                        && pcx_t.ymax < 480) {
                        this.palette = new int[768];

                        for (int int2 = 0; int2 < 768; int2++) {
                            if (int0 - 128 - 768 + int2 < pcx_t.data.length) {
                                this.palette[int2] = pcx_t.data[int0 - 128 - 768 + int2] & 255;
                            }
                        }

                        this.imageWidth = pcx_t.xmax + 1;
                        this.imageHeight = pcx_t.ymax + 1;
                        int[] int3 = new int[(pcx_t.ymax + 1) * (pcx_t.xmax + 1)];
                        this.pic = int3;
                        boolean boolean0 = false;
                        boolean boolean1 = false;
                        return this.pic != null && this.palette != null ? 1 : -1;
                    } else {
                        DebugLog.log("Bad pcx file " + string);
                        return -1;
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            return 1;
        }
    }

    private void loadPCXpal(int[] _int) {
        this.palette = _int;
    }

    class pcx_t {
        char bits_per_pixel;
        short bytes_per_line;
        char color_planes;
        byte[] data;
        char encoding;
        byte[] filler = new byte[58];
        short hres;
        short vres;
        char manufacturer;
        int[] palette = new int[48];
        short palette_type;
        char reserved;
        char version;
        short xmin;
        short ymin;
        short xmax;
        short ymax;

        pcx_t(byte[] _byte) {
            this.manufacturer = (char)_byte[0];
            this.version = (char)_byte[1];
            this.encoding = (char)_byte[2];
            this.bits_per_pixel = (char)_byte[3];
            this.xmin = (short)(_byte[4] + (_byte[5] << 8) & 0xFF);
            this.ymin = (short)(_byte[6] + (_byte[7] << 8) & 0xFF);
            this.xmax = (short)(_byte[8] + (_byte[9] << 8) & 0xFF);
            this.ymax = (short)(_byte[10] + (_byte[11] << 8) & 0xFF);
            this.hres = (short)(_byte[12] + (_byte[13] << 8) & 0xFF);
            this.vres = (short)(_byte[14] + (_byte[15] << 8) & 0xFF);

            for (int int0 = 0; int0 < 48; int0++) {
                this.palette[int0] = _byte[16 + int0] & 255;
            }

            this.reserved = (char)_byte[64];
            this.color_planes = (char)_byte[65];
            this.bytes_per_line = (short)(_byte[66] + (_byte[67] << 8) & 0xFF);
            this.palette_type = (short)(_byte[68] + (_byte[69] << 8) & 0xFF);

            for (int int1 = 0; int1 < 58; int1++) {
                this.filler[int1] = _byte[70 + int1];
            }

            this.data = new byte[_byte.length - 128];

            for (int int2 = 0; int2 < _byte.length - 128; int2++) {
                this.data[int2] = _byte[128 + int2];
            }
        }
    }
}
